package com.xuecheng.content.controller;

import com.xuecheng.api.content.TeachplanApi;
import com.xuecheng.api.content.model.dto.TeachplanDTO;
import com.xuecheng.api.content.model.dto.TeachplanMediaDTO;
import com.xuecheng.api.content.model.vo.TeachplanVO;
import com.xuecheng.api.media.vo.BindTeachplanMediaVO;
import com.xuecheng.content.common.util.UAASecurityUtil;
import com.xuecheng.content.convert.TeachPlanConvert;
import com.xuecheng.content.service.TeachplanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class TeachplanController implements TeachplanApi {


    @Autowired
    private TeachplanService teachplanService;

    // @GetMapping("teachplan/{courseId}")
    // public List<TeachPlanNode> trees(@PathVariable("courseId") Long courseId) {
    //     List<TeachPlanNode> nodes = teachplanMapper.selectTreeNodeByCourseId(courseId);
    //     return nodes;
    // }

    /**
     * 课程计划树形结构查询
     *
     * @param courseId
     * @return
     */
    @GetMapping("teachplan/{courseId}/tree-nodes")
    public TeachplanDTO queryTreeNodesByCourseId(@PathVariable("courseId") Long courseId) {
        Long companyId = UAASecurityUtil.getCompanyId();
        TeachplanDTO dto = teachplanService.queryTreeNodes(courseId, companyId);
        return dto;
    }

    /**
     * 新增或修改课程计划
     *
     * @param vo
     * @return
     */
    @PostMapping("teachplan")
    public TeachplanDTO createOrModifyTeachplan(@RequestBody TeachplanVO vo) {
        Long companyId = UAASecurityUtil.getCompanyId();
        TeachplanDTO dto = TeachPlanConvert.INSTANCE.vo2dto(vo);
        TeachplanDTO result = teachplanService.createOrModifyTeachplan(dto, companyId);
        return result;
    }

    /**
     * 删除课程大纲
     *
     * @param teachPlanId
     */
    @DeleteMapping("teachplan/{teachPlanId}")
    public void removeTeachPlan(@PathVariable("teachPlanId") Long teachPlanId) {
        Long companyId = UAASecurityUtil.getCompanyId();
        teachplanService.removeTeachPlan(teachPlanId, companyId);
    }

    /**
     * 课程计划绑定媒资信息
     *
     * @param vo
     * @return
     */
    @PostMapping("teachplan/media/association")
    public TeachplanMediaDTO associateMedia(@RequestBody BindTeachplanMediaVO vo) {
        Long companyId = UAASecurityUtil.getCompanyId();

        TeachplanMediaDTO mediaDTO = new TeachplanMediaDTO();
        mediaDTO.setTeachplanId(vo.getTeachplanId());
        mediaDTO.setMediaId(vo.getMediaId());

        TeachplanMediaDTO resultDTO = teachplanService.associateMedia(mediaDTO, companyId);
        return resultDTO;
    }

}
